package com.adminserver.util.stpInterface;

import cn.dev33.satoken.stp.StpInterface;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.adminserver.customEntity.UserRolePermissionEntity;
import com.adminserver.entity.PermissionEntity;
import com.adminserver.entity.UserPermissionEntity;
import com.adminserver.entity.UserRoleEntity;
import com.adminserver.mapper.PermissionMapper;
import com.adminserver.mapper.UserPermissionMapper;
import com.adminserver.mapper.UserRoleMapper;
import com.adminserver.service.UserPermissionService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 自定义权限验证接口扩展
 */
@Component    // 保证此类被SpringBoot扫描，完成Sa-Token的自定义权限验证扩展
public class StpInterfaceImpl implements StpInterface {
    @Autowired
    UserRoleMapper userRoleMapper;

    @Autowired
    PermissionMapper permissionMapper;

    @Autowired
    UserPermissionMapper userPermissionMapper;

    /**
     * 返回指定账号id所拥有的权限码集合
     *
     * @param loginId   账号id
     * @param loginType 账号类型
     * @return 该账号id具有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        List<String> list = permissionMapper.queryUserPermissionByUserId(loginId.toString());
        List<UserPermissionEntity> userPermissionEntities = userPermissionMapper.queryLimitUserPermissionByUserIds( Arrays.asList(loginId.toString()));
        if (userPermissionEntities.size() > 0){
            list.clear();
            for (UserPermissionEntity entity : userPermissionEntities) {
                list.add((entity.getPermName()));
            }
        }
        return list;
    }

    /**
     * 返回指定账号id所拥有的角色标识集合
     *
     * @param loginId   账号id
     * @param loginType 账号类型
     * @return 该账号id具有的角色标识集合
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        List<String> list = userRoleMapper.queryUserRoleByUserId(loginId.toString());
        this.getPermissionList(loginId, loginType);
        return list;
    }
}
